package cn.mandy.sysadmin.dto;

import cn.mandy.sysadmin.model.MandyParameter;
import cn.mandy.sysadmin.model.MandyRole;
import cn.mandy.sysadmin.model.MandyUser;
import com.alibaba.fastjson.annotation.JSONField;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

public class MandyUserDetail implements UserDetails {
    private MandyUser mandyUser;
    private List<MandyAuthority> authorities;
    private List<MandyRole> rolesAvailable;
    private List<MandyRole> rolesUnavailable;
    private String requestUrl;
    private List<MandyParameter> parameters;

    public List<MandyParameter> getParameters() {
        return parameters;
    }

    public void setParameters(List<MandyParameter> parameters) {
        this.parameters = parameters;
    }

    public String getRequestUrl() {
        return requestUrl;
    }

    public void setRequestUrl(String requestUrl) {
        this.requestUrl = requestUrl;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //通过 SysMmUser.current_role_id 取得该role下所有的permission;
        return authorities;
    }

    public void setAuthorities(List<MandyAuthority> authorities) {
        this.authorities = authorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return ! mandyUser.getUserState().equals("3");
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.mandyUser.getIzEnabled().equals("1");
    }

    public MandyUser getMandyUser() {
        return this.mandyUser;
    }

    public void setMandyUser(MandyUser mandyUser) {
        this.mandyUser = mandyUser;
    }

    @JSONField(serialize=false)
    @Override
    public String getPassword() {
        return this.mandyUser.getPassword();
    }

    @Override
    public String getUsername() {
        return this.mandyUser.getUsername();
    }

    public List<MandyRole> getRolesAvailable() {
        return rolesAvailable;
    }

    public void setRolesAvailable(List<MandyRole> rolesAvailable) {
        this.rolesAvailable = rolesAvailable;
    }

    public List<MandyRole> getRolesUnavailable() {
        return rolesUnavailable;
    }

    public void setRolesUnavailable(List<MandyRole> rolesUnavailable) {
        this.rolesUnavailable = rolesUnavailable;
    }
}
